package com.blb.zy_community.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zy_community.entity.ResponseResult;
import com.blb.zy_community.entity.ZyBuilding;
import com.blb.zy_community.entity.ZyCommunity;
import com.blb.zy_community.service.IZyBuildingService;
import com.blb.zy_community.service.IZyCommunityService;
import com.blb.zy_community.service.impl.ZyBuildingServiceImpl;
import com.blb.zy_community.service.impl.ZyCommunityServiceImpl;
import com.mysql.cj.PingTarget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 楼栋  前端控制器
 * </p>
 *
 * @author 二组
 * @since 2023-06-18
 */
@CrossOrigin
@RestController

public class ZyBuildingController {
    @Autowired
    private IZyBuildingService zyBuildingService;
    @Autowired
    private IZyCommunityService zyCommunityService;
    /*
    * 页面分页展示
    * */
    @GetMapping("building-page")
    public ResponseResult<Page<ZyBuilding>> getByBuildingPage(@RequestParam(required = false,defaultValue = "1") Long current,
                                                              @RequestParam(required = false,defaultValue = "5") Long size,
                                                              @RequestParam(required = false,defaultValue = "") String buildingName,
                                                              @RequestParam(required = false) String buildingCode,
                                                              @RequestParam(required = false) Long communityId
    ){
        Page<ZyBuilding> byBuilding = zyBuildingService.getByBuilding(current,size,buildingName,buildingCode,communityId);
        return ResponseResult.ok(byBuilding);
    }
    //添加
    @PostMapping("/building")
    public ResponseResult<String> addBuilding(@RequestBody ZyBuilding zyBuilding){
        zyBuildingService.save(zyBuilding);
        return ResponseResult.ok(zyBuilding);
    }
    //删除
    @DeleteMapping("/building/{buildingId}")
    public ResponseResult<String> deleteBuilding(@PathVariable Long buildingId){
        zyBuildingService.removeById(buildingId);
        return ResponseResult.ok(buildingId);
    }
//    修改
    @PutMapping("/building")
    public ResponseResult<String> updateBuild(@RequestBody ZyBuilding zyBuilding){
        zyBuildingService.updateById(zyBuilding);
        return ResponseResult.ok(zyBuilding);
    }
/*批量删除*/
    @DeleteMapping("/builds/{buildIds}")
    public ResponseResult<String> deleteBuilds(@PathVariable Long[] buildIds){
        zyBuildingService.deleteBuilds(buildIds);
        return ResponseResult.ok(buildIds);
    }
    @RequestMapping("/building/{id}")
    public ResponseResult<String> getOneBuilding(@PathVariable Long id){
        ZyBuilding buildingId = zyBuildingService.getOne(new QueryWrapper<ZyBuilding>().eq("building_id", id));
        return ResponseResult.ok(buildingId);
    }

//    @RequestMapping("communityList")
//    public ResponseResult<String> communityList(){
//        List<ZyCommunity> list = zyCommunityService.list();
//        return ResponseResult.ok(list);
//    }
//    @RequestMapping("communityList")
//    public ResponseResult<String> communityList(){
//        List<ZyCommunity> list = zyCommunityService.list();
//        return ResponseResult.ok(list);
//    }

    /**
     * 根据楼栋查询小区
     */
    @GetMapping("communityByBuilding")
    public ResponseResult<String> getCommunityByBuildingById(@RequestParam(required = false) Long id){
        List<ZyBuilding> communityId = zyBuildingService.list(new QueryWrapper<ZyBuilding>().eq("community_id", id));
        return ResponseResult.ok(communityId);
    }

}
